package mobilesmil.adapter;

import java.util.ArrayList;

import mobilesmil.activity.R;
import mobilesmil.activity.composer.ComposerActivity;
import mobilesmil.data.SmilResource;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * {@link ArrayAdapter}<{@link SmilResource}> used by {@link ComposerActivity} to display each line of the list of {@link SmilResource}s.
 * Displays the resource's type, file name, begin time, end time, and a delete "X" button.
 * @author Ryan Ware
 *
 */
public class SmilResourceAdapter extends ArrayAdapter<SmilResource> {

    private Context mContext;
    private ArrayList<SmilResource> mItems;
    
    /**
     * Creates an {@link Adapter} to display a list of {@link SmilResource}s.
     * @param context {@link Context} of this {Alink Adapter}
     * @param textViewResourceId id of the XML layout for each row of the list
     * @param items {@link ArrayList}<{@link SmilResource}> list to be displayed
     */
    public SmilResourceAdapter(Context context, int textViewResourceId, ArrayList<SmilResource> items) {
            super(context, textViewResourceId, items);
            this.mContext = context;
            this.mItems = items;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            final int pos = position;
            if (view == null) { // if no View provided
                LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = vi.inflate(R.layout.list_resources_row, null);
            }
            
            SmilResource resource = mItems.get(position);
            if (resource != null) 
            { // retrieve all properties of this SmilResource to be displayed in the row
                    TextView textSource = (TextView) view.findViewById(R.id.textSource);
                    TextView textTimes = (TextView) view.findViewById(R.id.textTimes);
                    ImageView icon = (ImageView) view.findViewById(R.id.iconResourceType);
                    ImageView deleteIcon = (ImageView) view.findViewById(R.id.iconDelete);
                    
                    if (textSource != null) {
                          textSource.setText(resource.getSource());
                    }
                    if(textTimes != null) {
                          textTimes.setText("Begin: " + resource.getBeginTime() + "   End: " + resource.getEndTime());
                    }
                    if(icon != null) {
                    	if(resource.getType().equals(SmilResource.TYPE_AUDIO))
                    		icon.setImageResource(R.drawable.audio_icon);
                    	else if(resource.getType().equals(SmilResource.TYPE_IMAGE))
                    		icon.setImageResource(R.drawable.image_icon);
                    	else if(resource.getType().equals(SmilResource.TYPE_TEXT))
                    		icon.setImageResource(R.drawable.text_icon);
                    	else if(resource.getType().equals(SmilResource.TYPE_VIDEO))
                    		icon.setImageResource(R.drawable.video_icon);
                    }
                    if(deleteIcon != null) {
                    	deleteIcon.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								((ComposerActivity) v.getContext()).promptToDeleteResource(pos);
							}
						});
                    }
            }
            return view;
    }
            
}
